package hu.berczi.projects.ImageProcessing.GUI;

import hu.berczi.projects.ImageProcessing.ImageProcessing;
import hu.berczi.projects.ImageProcessing.ImageProcessing.resizeMethodType;

import java.io.File;

import javax.media.jai.operator.TransposeDescriptor;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ImageProcessingGUI extends org.eclipse.swt.widgets.Composite {

	private ImageProcessing imageProcessing;
	private Button GO;
	private Button enableGUI;
	private Button rotateLeft;
	private Button enableRotate;
	private Button enableRotateEXIF;
	private Group Group_rotate;
	private Group group3;
	private Group group2;
	private Text enlargeMaxPerc;
	private StyledText enlargeMax;
	private Text enlargemaxText;
	private Button enableEnlarge;
	private Text DisplaySizeText;
	private Button resizeMethod_JAIMultiple;
	private Button resizeMethod_JAIAffine;
	private Button resizeMethod_JAIScale;
	private Group group_ResizeMethod;
	private Menu menu2;
	private MenuItem HelpMenu;
	private Menu menu1;
	private MenuItem FileMenu;
	private Menu menu;
	private Button resizeMethod_NConvert;
	private Group ResizeGroup;
	private Button BUTTON_browse_source;
	private Button resizeMethod_Graphics;
	private Text targetHeight;
	private Label x;
	private StyledText targetWidth;
	private Text sourceText;
	private Button enableOverWrite;
	private Button rotateRight;

	/**
	 * Auto-generated main method to display this
	 * org.eclipse.swt.widgets.Composite inside a new Shell.
	 */
	public static void main(String[] args) {
		showGUI();
	}

	/**
	 * Overriding checkSubclass allows this class to extend
	 * org.eclipse.swt.widgets.Composite
	 */
	protected void checkSubclass() {
	}

	/**
	 * Auto-generated method to display this org.eclipse.swt.widgets.Composite
	 * inside a new Shell.
	 */
	public static void showGUI() {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		ImageProcessingGUI inst = new ImageProcessingGUI(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if (size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public ImageProcessingGUI(org.eclipse.swt.widgets.Composite parent,
			int style) {
		super(parent, style);
		initImageProcessing();
		initGUI();
	}

	private void initImageProcessing() {
		imageProcessing = new ImageProcessing();
		imageProcessing.setEnableGui(false);
		imageProcessing.addSourceDir(new File("d:\\himzes"));
	}

	private void toGUI() {

		// Rotate
		enableRotate.setSelection(imageProcessing.isEnableRotate_Display());
		enableRotateEXIF.setSelection(imageProcessing.isEnableRotate_EXIF());

		sourceText.setText(ImageProcessing.getSourceDir(0).getAbsolutePath());

		enableEnlarge.setSelection(imageProcessing.isEnableEnlarge());
		enableGUI.setSelection(imageProcessing.isEnableGUI());

		resizeMethodType rm = imageProcessing.getResizeMethod();

		if (rm.ordinal() == ImageProcessing.resizeMethodType.nConvert.ordinal()) {
			resizeMethod_NConvert.setSelection(true);
		} else if (rm.ordinal() == ImageProcessing.resizeMethodType.JAIaffine
				.ordinal()) {
			resizeMethod_JAIAffine.setSelection(true);
		} else if (rm.ordinal() == ImageProcessing.resizeMethodType.JAIscale
				.ordinal()) {
			resizeMethod_JAIScale.setSelection(true);
		} else if (rm.ordinal() == ImageProcessing.resizeMethodType.JAIscaleMultiple
				.ordinal()) {
			resizeMethod_JAIMultiple.setSelection(true);
		}

		// Rotating
		if (imageProcessing.isEnableRotate_Display()) {
			if (imageProcessing.getRotateMode() == TransposeDescriptor.ROTATE_90) {
				rotateRight.setSelection(true);
			} else
				rotateLeft.setSelection(true);
		} else {
			rotateLeft.setEnabled(false);
			rotateRight.setEnabled(false);
		}

	}

	private void initGUI() {
		try {
			this.setLayout(new FormLayout());
			Display display = new Display();
			final Shell shell = new Shell(display);
			{
				BUTTON_browse_source = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData BUTTON_browse_sourceLData = new FormData();
				BUTTON_browse_sourceLData.top = new FormAttachment(0, 1000, 192);
				BUTTON_browse_sourceLData.width = 14;
				BUTTON_browse_sourceLData.height = 13;
				BUTTON_browse_sourceLData.left = new FormAttachment(0, 1000, 93);
				BUTTON_browse_source.setLayoutData(BUTTON_browse_sourceLData);
				BUTTON_browse_source.setText("..");
				BUTTON_browse_source
						.addSelectionListener(new SelectionAdapter() {
							public void widgetSelected(SelectionEvent e) {
								FileDialog dialog = new FileDialog(shell,
										SWT.NULL);
								String path = dialog.open();
								if (path != null) {

									File file = new File(path);
									//if (file.isFile())
										//displayFiles(new String[] { file.toString() });
									//else
										//displayFiles(file.list());

								}
							}
						});
			}
			{
				FormData sourceTextLData = new FormData();
				sourceTextLData.width = 64;
				sourceTextLData.height = 13;
				sourceTextLData.left = new FormAttachment(0, 1000, 17);
				sourceTextLData.top = new FormAttachment(0, 1000, 192);
				sourceText = new Text(this, SWT.NONE);
				sourceText.setLayoutData(sourceTextLData);
			}
			{
				enableOverWrite = new Button(this, SWT.CHECK | SWT.LEFT);
				FormData enableOverWriteLData = new FormData();
				enableOverWriteLData.width = 69;
				enableOverWriteLData.height = 16;
				enableOverWriteLData.left = new FormAttachment(0, 1000, 252);
				enableOverWriteLData.top = new FormAttachment(0, 1000, 164);
				enableOverWrite.setLayoutData(enableOverWriteLData);
				enableOverWrite.setText("Overwrite");
			}
			{
				menu = new Menu(getShell(), SWT.BAR);
				getShell().setMenuBar(menu);
				{
					FileMenu = new MenuItem(menu, SWT.CASCADE);
					FileMenu.setText("File");
					{
						menu1 = new Menu(FileMenu);
						FileMenu.setMenu(menu1);
					}
				}
				{
					HelpMenu = new MenuItem(menu, SWT.CASCADE);
					HelpMenu.setText("Help");
					HelpMenu.setSelection(false);
					{
						menu2 = new Menu(HelpMenu);
						HelpMenu.setMenu(menu2);
					}
				}
			}
			{
				ResizeGroup = new Group(this, SWT.NONE);
				ResizeGroup.setLayout(null);
				FormData ResizeGroupLData = new FormData();
				ResizeGroupLData.width = 340;
				ResizeGroupLData.height = 117;
				ResizeGroupLData.top = new FormAttachment(0, 1000, 2);
				ResizeGroupLData.left = new FormAttachment(0, 1000, 5);
				ResizeGroup.setLayoutData(ResizeGroupLData);
				ResizeGroup.setText("Resize");
				{
					group3 = new Group(ResizeGroup, SWT.NONE);
					group3.setLayout(null);
					group3.setBounds(129, 59, 210, 38);
					{
						enableEnlarge = new Button(group3, SWT.CHECK | SWT.LEFT);
						enableEnlarge.setText("Enlarge");
						enableEnlarge.setBounds(6, 16, 57, 16);
						enableEnlarge
								.addSelectionListener(new SelectionAdapter() {
									public void widgetSelected(
											SelectionEvent evt) {
										boolean sel = enableEnlarge
												.getSelection();
										imageProcessing.setEnableResize(sel);
										enlargeMax.setVisible(sel);
										enlargeMaxPerc.setVisible(sel);
										enlargemaxText.setVisible(sel);
									}
								});
						enableEnlarge.addFocusListener(new FocusAdapter() {
							public void focusGained(FocusEvent evt) {
								System.out
										.println("enableEnlarge.focusGained, event="
												+ evt);
								// TODO add your code for
								// enableEnlarge.focusGained
							}
						});
					}
					{
						enlargemaxText = new Text(group3, SWT.NONE);
						enlargemaxText.setText("maximum:");
						enlargemaxText.setDoubleClickEnabled(false);
						enlargemaxText.setEditable(false);
						enlargemaxText.setBounds(66, 17, 54, 13);
					}
					{
						enlargeMax = new StyledText(group3, SWT.NONE);
						enlargeMax.setBounds(123, 17, 65, 13);
					}
					{
						enlargeMaxPerc = new Text(group3, SWT.NONE);
						enlargeMaxPerc.setText("%");
						enlargeMaxPerc.setDoubleClickEnabled(false);
						enlargeMaxPerc.setEditable(false);
						enlargeMaxPerc.setBounds(191, 17, 17, 13);
					}
					group3.setTabList(new org.eclipse.swt.widgets.Control[] {
							enableEnlarge, enlargeMax });
				}
				{
					group2 = new Group(ResizeGroup, SWT.NONE);
					group2.setLayout(null);
					group2.setEnabled(false);
					group2.setBounds(129, 15, 131, 38);
					{
						DisplaySizeText = new Text(group2, SWT.NONE);
						DisplaySizeText.setText("Resolution");
						DisplaySizeText.setEditable(false);
						DisplaySizeText.setToolTipText("Output resolution");
						DisplaySizeText.setOrientation(SWT.HORIZONTAL);
						DisplaySizeText.setBounds(5, 15, 56, 14);
					}
					{
						targetWidth = new StyledText(group2, SWT.NONE);
						targetWidth.setText("240");
						targetWidth.setBounds(67, 14, 24, 14);
						targetWidth.setDragDetect(false);
					}
					{
						x = new Label(group2, SWT.NONE);
						x.setText("x");
						x.setBounds(93, 14, 11, 14);
					}
					{
						targetHeight = new Text(group2, SWT.NONE);
						targetHeight.setText("320");
						targetHeight.setBounds(101, 14, 25, 14);
					}
					group2.setTabList(new org.eclipse.swt.widgets.Control[] {
							targetWidth, targetHeight });
				}
				{
					group_ResizeMethod = new Group(ResizeGroup, SWT.NONE);
					RowLayout group1Layout = new RowLayout(
							org.eclipse.swt.SWT.HORIZONTAL);
					group_ResizeMethod.setLayout(group1Layout);
					group_ResizeMethod.setText("Method");
					group_ResizeMethod.setBounds(9, 15, 108, 115);
					{
						resizeMethod_NConvert = new Button(group_ResizeMethod,
								SWT.RADIO | SWT.LEFT);
						resizeMethod_NConvert.setText("nConvert");
						resizeMethod_NConvert.setToolTipText("msdkmklsmdf");
						RowData resizeMethod_NConvertLData = new RowData();
						resizeMethod_NConvert
								.setLayoutData(resizeMethod_NConvertLData);
						resizeMethod_NConvert.setBounds(6, 17, 66, 16);
						resizeMethod_NConvert
								.addSelectionListener(new SelectionAdapter() {
									public void widgetSelected(
											SelectionEvent evt) {
										imageProcessing
												.setResizeMethod(ImageProcessing.resizeMethodType.nConvert);
									}
								});
					}
					{
						resizeMethod_Graphics = new Button(group_ResizeMethod,
								SWT.RADIO | SWT.LEFT);
						resizeMethod_Graphics.setText("Graphics");
						RowData resizeMethod_GraphicsLData = new RowData();
						resizeMethod_GraphicsLData.width = 65;
						resizeMethod_GraphicsLData.height = 16;
						resizeMethod_Graphics
								.setLayoutData(resizeMethod_GraphicsLData);
						resizeMethod_Graphics.setBounds(6, 74, 65, 16);
						resizeMethod_Graphics
								.addSelectionListener(new SelectionAdapter() {
									public void widgetSelected(
											SelectionEvent evt) {
										imageProcessing
												.setResizeMethod(ImageProcessing.resizeMethodType.GraphicsHD);
									}
								});
					}
					{
						resizeMethod_JAIScale = new Button(group_ResizeMethod,
								SWT.RADIO | SWT.LEFT);
						resizeMethod_JAIScale.setText("JAIscale");
						resizeMethod_JAIScale.setToolTipText("msdkmklsmdf");
						RowData resizeMethod_JAIScaleLData = new RowData();
						resizeMethod_JAIScaleLData.width = 61;
						resizeMethod_JAIScaleLData.height = 16;
						resizeMethod_JAIScale
								.setLayoutData(resizeMethod_JAIScaleLData);
						resizeMethod_JAIScale.setBounds(6, 55, 61, 16);
						resizeMethod_JAIScale
								.addSelectionListener(new SelectionAdapter() {
									public void widgetSelected(
											SelectionEvent evt) {
										imageProcessing
												.setResizeMethod(ImageProcessing.resizeMethodType.JAIscale);
									}
								});
					}
					{
						resizeMethod_JAIAffine = new Button(group_ResizeMethod,
								SWT.RADIO | SWT.LEFT);
						resizeMethod_JAIAffine.setText("JAIaffine");
						resizeMethod_JAIAffine.setToolTipText("msdkmklsmdf");
						RowData resizeMethod_JAIAffineLData = new RowData();
						resizeMethod_JAIAffineLData.width = 65;
						resizeMethod_JAIAffineLData.height = 16;
						resizeMethod_JAIAffine
								.setLayoutData(resizeMethod_JAIAffineLData);
						resizeMethod_JAIAffine.setBounds(6, 74, 65, 16);
						resizeMethod_JAIAffine
								.addSelectionListener(new SelectionAdapter() {
									public void widgetSelected(
											SelectionEvent evt) {
										imageProcessing
												.setResizeMethod(ImageProcessing.resizeMethodType.JAIaffine);
									}
								});

					}
					{
						resizeMethod_JAIMultiple = new Button(
								group_ResizeMethod, SWT.RADIO | SWT.LEFT);
						resizeMethod_JAIMultiple.setText("JAIscaleMultiple");
						resizeMethod_JAIMultiple.setToolTipText("msdkmklsmdf");
						RowData resizeMethod_JAIMultipleLData = new RowData();
						resizeMethod_JAIMultipleLData.width = 97;
						resizeMethod_JAIMultipleLData.height = 16;
						resizeMethod_JAIMultiple
								.setLayoutData(resizeMethod_JAIMultipleLData);
						resizeMethod_JAIMultiple.setBounds(6, 36, 97, 16);
						resizeMethod_JAIMultiple
								.addSelectionListener(new SelectionAdapter() {
									public void widgetSelected(
											SelectionEvent evt) {
										imageProcessing
												.setResizeMethod(ImageProcessing.resizeMethodType.JAIscaleMultiple);
									}
								});
					}
					group_ResizeMethod
							.setTabList(new org.eclipse.swt.widgets.Control[] {
									resizeMethod_NConvert,
									resizeMethod_JAIMultiple,
									resizeMethod_JAIScale,
									resizeMethod_JAIAffine });
				}
				ResizeGroup.setTabList(new org.eclipse.swt.widgets.Control[] {
						group_ResizeMethod, group2, group3 });
			}
			{
				enableGUI = new Button(this, SWT.CHECK | SWT.LEFT);
				FormData enableGUICheckBoxLData = new FormData();
				enableGUICheckBoxLData.width = 98;
				enableGUICheckBoxLData.height = 16;
				enableGUICheckBoxLData.left = new FormAttachment(0, 1000, 252);
				enableGUICheckBoxLData.top = new FormAttachment(0, 1000, 141);
				enableGUI.setLayoutData(enableGUICheckBoxLData);
				enableGUI.setText("Preview");
			}
			{
				GO = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData GOLData = new FormData();
				GOLData.width = 60;
				GOLData.height = 22;
				GOLData.right = new FormAttachment(1000, 1000, -19);
				GOLData.bottom = new FormAttachment(1000, 1000, -12);
				GO.setLayoutData(GOLData);
				GO.setText("GO");
				GO.setBounds(210, 178, 60, 22);
				GO.setAlignment(SWT.CENTER);
			}
			{
				Group_rotate = new Group(this, SWT.NONE);
				Group_rotate.setLayout(null);
				FormData Group_rotateLData = new FormData();
				Group_rotateLData.width = 228;
				Group_rotateLData.height = 40;
				Group_rotateLData.left = new FormAttachment(0, 1000, 9);
				Group_rotateLData.top = new FormAttachment(0, 1000, 132);
				Group_rotateLData.bottom = new FormAttachment(1000, 1000, -42);
				Group_rotate.setLayoutData(Group_rotateLData);
				Group_rotate.setText("Rotate");
				{
					enableRotate = new Button(Group_rotate, SWT.CHECK
							| SWT.LEFT);
					enableRotate.setText("Rotating to display");
					enableRotate.setBounds(6, 17, 111, 16);
					enableRotate.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							boolean sel = enableRotate.getSelection();
							imageProcessing.setEnableRotate_Display(sel);
							rotateLeft.setVisible(sel);
							rotateRight.setVisible(sel);
						}
					});
				}
				{
					enableRotateEXIF = new Button(Group_rotate, SWT.CHECK
							| SWT.LEFT);
					enableRotateEXIF.setText("EXIF");
					enableRotateEXIF.setBounds(6, 35, 43, 16);
					enableRotateEXIF
							.setToolTipText("Rotating according to the EXIF info of the picture.");
					enableRotateEXIF
							.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									boolean sel = enableRotateEXIF
											.getSelection();
									imageProcessing.setEnableRotate_EXIF(sel);
								}
							});
				}
				{
					rotateLeft = new Button(Group_rotate, SWT.RADIO | SWT.LEFT);
					rotateLeft.setText("Left");
					rotateLeft.setBounds(119, 15, 40, 21);
				}
				{
					rotateRight = new Button(Group_rotate, SWT.RADIO | SWT.LEFT);
					rotateRight.setText("Right");
					rotateRight.setBounds(163, 11, 60, 30);
					// rotateRight.setGrayed(true);
				}
				Group_rotate
						.setTabList(new org.eclipse.swt.widgets.Control[] {
								enableRotate, rotateLeft, rotateRight,
								enableRotateEXIF });
			}
			this.layout();
			pack();
			this.setDragDetect(false);
		} catch (Exception e) {
			e.printStackTrace();
		}
		toGUI();
	}

	public Button getEnableGUI() {
		return enableGUI;
	}

	private void enableEnlargeCheckboxFocusGained(FocusEvent evt) {
		System.out.println("enableEnlarge.focusGained, event=" + evt);
		// TODO add your code for enableEnlarge.focusGained
	}

}
